<?php
namespace app\material\model;
use app\BaseModel;
use app\card\model\Company;
use app\shop\model\AdminCompany;
use think\facade\Db;



class BrochureShare extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_material_brochure_share';


    /**
     * @author chenniang
     * @DataTime: 2021-07-06 13:47
     * @功能说明:员工排行榜
     */

    public function staffTopList($dis,$type=0,$page=10){

        $order = $type==0?'share_times desc,a.id desc':'get_user_num desc,a.id desc';

        $data = $this->alias('a')
                ->join('longbing_card_user_info b','a.staff_id = b.fans_id')
                ->where($dis)
                ->field('a.*,COUNT(a.id) as share_times,COUNT(distinct(a.user_id)) as get_user_num')
                ->group('a.staff_id')
                ->order($order)
                ->paginate($page)
                ->toArray();

        if(!empty($data['data'])){

            $company_model = new Company();

            foreach ($data['data'] as &$v){

                $v['staff_name']   = lbUserName($v['staff_id']);

                $v['staff_avatar'] = lbUserAvatar($v['staff_id']);

                $v['company_name'] = $company_model->where(['id'=>$v['company_id']])->value('name');

            }

        }

        if($type==0){
            //总的次数
            $data['total_num'] = $this->alias('a')->where($dis)->count();

        }else{
            //总的次数
            $data['total_num'] = $this->alias('a')->where($dis)->group('a.staff_id,a.user_id')->count();
        }

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-06 14:34
     * @功能说明:分享册数
     */
    public function shareBrochure($dis,$type){

        $data = $this->alias('a')
                ->join('longbing_card_material_brochure b','a.b_id = b.id')
                ->where($dis)
                ->where(['a.type'=>$type,'b.status'=>1])
                ->group('a.b_id')
                ->count();

        return $data;

    }

    /**
     * @author chenniang
     * @DataTime: 2021-07-06 14:34
     * @功能说明:分享人数/获客人数
     */
    public function shareUser($dis,$type){

        $data = $this->alias('a')
            ->join('longbing_card_material_brochure b','a.b_id = b.id')
            ->where($dis)
            ->where(['a.type'=>$type,'b.status'=>1])
            ->group('a.user_id')
            ->count();

        return $data;

    }

    /**
     * @author chenniang
     * @DataTime: 2021-07-06 14:34
     * @功能说明:分享人数/获客人数
     */
    public function shareTime($dis,$type){

        $data = $this->alias('a')
            ->join('longbing_card_material_brochure b','a.b_id = b.id')
            ->where($dis)
            ->where(['a.type'=>$type,'b.status'=>1])
            ->group('a.id')
            ->count();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-12 11:18
     * @功能说明:
     */

    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



}